//
//  DYHomeViewController.swift
//  Douyu_Swift
//
//  Created by Albert Zhu on 16/11/1.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

private let kTitleViewH: CGFloat = 40

class DYHomeViewController: UIViewController {

    lazy var pageTitleView: DYPageTitleView = {
        let titleFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐", "游戏", "娱乐", "资讯"]
        let titleView = DYPageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    lazy var pageContentView: DYPageContentView = {[weak self] in
        let kContentH = kScreenH - kStatusBarH - kNavigationBarH - kTitleViewH
        let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH + kTitleViewH, width: kScreenH, height: kContentH)
        
        var childVCs = [UIViewController]()
        childVCs.append(DYRecommendController())
        
        for _ in 0..<4 {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor.randomColor
            childVCs.append(vc)
        }
        
        let contentView = DYPageContentView(frame: contentFrame, childVCs: childVCs, parentController: self)
        contentView.backgroundColor = UIColor.randomColor
        contentView.delegate = self
        
        return contentView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
}

// MARK: - UI相关
extension DYHomeViewController {
    func setupUI() {
        setupNav()
        
        view.addSubview(pageTitleView)
        automaticallyAdjustsScrollViewInsets = false
        view.addSubview(pageContentView)
    }
    
    /// 设置导航栏UI
    func setupNav() {
        
        let rightBtnSize = CGSize(width: 40, height: 40)
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "logo"), for: [])
        leftBtn.sizeToFit()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        let historyItem = UIBarButtonItem(dy_createItemImage: "image_my_history", highlightedImage: "Image_my_history_click", size: rightBtnSize)
        let searchItem = UIBarButtonItem(dy_createItemImage: "btn_search", highlightedImage: "btn_search_clicked", size: rightBtnSize)
        let qrcodeItem = UIBarButtonItem(dy_createItemImage: "Image_scan", highlightedImage: "Image_scan_click", size: rightBtnSize)
        
        navigationItem.rightBarButtonItems = [historyItem, searchItem, qrcodeItem]
    }
}

// MARK: - DYPageTitleViewDelegate
extension DYHomeViewController: DYPageTitleViewDelegate {
    func pageTitleView(_ titleView: DYPageTitleView, selectedIndex: Int) {
        
        pageContentView.setCurrentIndex(selectedIndex)
    }
}

// MARK: - DYPageContentViewDelegate
extension DYHomeViewController: DYPageContentViewDelegate {
    func pageContentView(_ pageContentView: DYPageContentView, scrollProgress: CGFloat) {
        pageTitleView.setLine(progress: scrollProgress)
    }
}
